/*
 * @Author: silas 
 * @Date: 2021-06-04 14:56:14 
 * @Last Modified by: silas
 * @Last Modified time: 2021-06-04 15:40:10
 */
<template>
    <div class="clearing">
        <el-form :inline="true" :model="form">
            <el-form-item label="年度" label-width="60px">
                <el-date-picker
                    v-model="form.year"
                    type="year"
                    size="mini"
                    placeholder="选择年"
                    value-format="yyyy"
                ></el-date-picker>
            </el-form-item>
            <el-form-item>
                <el-button size="mini" type="primary" @click="handle_search()"
                    >查询</el-button
                >
                <el-button @click="handle_down()" size="mini"
                    >模板下载</el-button
                >
                <el-button
                    type="orange"
                    @click="
                        dialogVisible = true;
                        importForm.year = '';
                        fileList = [];
                    "
                    size="mini"
                    >导入</el-button
                >
                <!-- <el-button size="mini" type="orange" @click="handle_export()"
                    >导出</el-button
                > -->
            </el-form-item>
        </el-form>
        <div class="tableBox">
            <el-table
                id="table1"
                ref="table"
                size="mini"
                :header-cell-style="{ background: '#eef1f6' }"
                :data="tableData"
                v-loading="tableLoading"
                style="width: 100%"
                border
            >
                <el-table-column prop="acctMonth" label="日期" min-width="120" />
                <el-table-column prop="areaName" label="区域" min-width="120" />
                <!-- <el-table-column
                    prop="title"
                    label="标题"
                    width="120"
                    show-overflow-tooltip
                /> -->
                <el-table-column
                    prop="importUserName"
                    label="导入人"
                     min-width="120"
                />
                <el-table-column
                    prop="importTime"
                    label="导入时间"
                     min-width="120"
                />
                <!-- <el-table-column
                    prop="descript"
                    label="说明"
                    min-width="140"
                    show-overflow-tooltip
                /> -->
                <el-table-column label="操作" width="180">
                    <template slot-scope="scope">
                        <!-- <el-button
                            type="text"
                            size="mini"
                            @click="handle_detail(scope.row)"
                            >详情</el-button
                        > -->
                        <el-button
                            type="text"
                            size="mini"
                            @click="handle_del(scope.row)"
                            >删除</el-button
                        >
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <el-dialog
            title="导入文件"
            append-to-body
            :visible.sync="dialogVisible"
            width="450px"
        >
            <el-form :model="importForm" ref="importForm" :rules="rules">
                <el-form-item label="日期" prop="year" label-width="80px">
                    <el-date-picker
                        v-model="importForm.year"
                        type="month"
                        size="mini"
                        placeholder="选择日期"
                        value-format="yyyyMM"
                    ></el-date-picker>
                </el-form-item>
                <el-form-item label="上传文件">
                    <el-upload
                        :limit="1"
                        accept=".xls, .xlsx"
                        class="avatar-uploader"
                        :auto-upload="false"
                        :file-list="fileList"
                        :on-change="changeFile"
                        action
                    >
                        <el-button size="mini" type="primary"
                            >点击上传</el-button
                        >
                    </el-upload>
                </el-form-item>
            </el-form>

            <div slot="footer">
                <div class="dialog-footer">
                    <el-button
                        size="mini"
                        type="primary"
                        @click="handle_import()"
                        >确定</el-button
                    >
                    <el-button size="mini" @click="dialogVisible = false"
                        >取消</el-button
                    >
                </div>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { $budgetAPI } from "@/api/";
import { getSessionStorage } from "@/common/js/storage";
import prefixAPI from "@/api/prefixAPI";
import { getCookie } from "@/common/js/cookie";
import FileSaver from "file-saver";
import XLSX from "xlsx";

export default {
    name: "clearing-tab1",
    data() {
        return {
            accountInfo: "",
            form: {
                areaId: "",
                year: this.moment().format('yyyy'),
            },
            tableLoading: false,
            tableData: [],
            titles: [],
            importForm: {
                year: "",
                title: "",
                description: "",
            },
            dialogVisible: false,
            fileList: [],
            rules: {
                year: [
                    { required: true, message: "请选择月份", trigger: "blur" },
                ],
            },
            param: "",
        };
    },
    mounted() {},
    methods: {
        init() {
            var that = this;

            that.accountInfo = JSON.parse(getSessionStorage("accountInfo"));
            that.getData();
        },
        getData() {
            var that = this,
                titles = [];

            that.tableLoading = true;
            $budgetAPI.getIctImportRecordList(
                {
                    acctYear: that.form.year,
                    token: getCookie("token"),
                },
                (res) => {
                    if (res.code == 0) {
                        that.tableData = res.content;
                    }
                    that.tableLoading = false;
                }
            );
        },
        handle_search() {
            var that = this;

             if (!that.form.year) {
                this.$message({
                    type: "error",
                    message: "请选择时间！",
                });
                return;
            }

            that.getData();
        },
        handle_export() {
            var xlsxParam = {
                raw: true,
            }; //转换成excel时，使用原始的格式，这样导出的时候数字过长不会变成科学计数法
            let workbook = XLSX.utils.book_new();
            let ws1 = XLSX.utils.table_to_sheet(
                document.querySelector("#table1"),
                xlsxParam
            );
            XLSX.utils.book_append_sheet(workbook, ws1, "预算画饼记录");
            let wbout = XLSX.write(workbook, {
                bookType: "xlsx",
                bookSST: true,
                type: "array",
            });
            try {
                FileSaver.saveAs(
                    new Blob([wbout], {
                        type: 'application/octet-stream;charset=utf-8"',
                    }),
                    "预算画饼记录.xlsx"
                );
            } catch (e) {
                if (typeof console !== "undefined") console.log(e, wbOut);
            }
            return wbout;
        },
        handle_detail(row) {
            // circle-tab2 
            this.$router.push({
                path: "/budget/circleDetail",
                query: { id: row.id },
            });
        },
        handle_del(row) {
            var that = this;

            that.$confirm("是否删除?", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            }).then(() => {
                $budgetAPI.deleteIctImportRecordById(
                    {
                        recordId: row.id,
                        token: getCookie("token"),
                    },
                    (res) => {
                        if (res.code == 0) {
                            this.$message({
                                type: "success",
                                message: "删除成功!",
                            });
                        } else {
                            this.$message({
                                type: "error",
                                message: res.message,
                            });
                        }
                        that.getData();
                    }
                );
            });
        },
        handle_down() {
            var that = this,
                token = getCookie("token"),
                acctYear = that.form.year;

            // if (!acctYear) {
            //     this.$message({
            //         type: "error",
            //         message: "请选择年份！",
            //     });
            //     return;
            // }
            window.location.href =
                prefixAPI.hp +
                "/api/ictGT/downTemplate?&acctYear=" +
                acctYear +
                "&token=" +
                token;
        },
        // 批量导入上传文件传递数据
        changeFile(file, fileList) {
            this.fileList = fileList;
            this.param = new FormData();
            this.param.append("file", file.raw);
        },
        // 导入
        handle_import(type) {
            var that = this;

            that.$refs["importForm"].validate((valid) => {
                if (valid) {
                    if (that.fileList.length == 0) {
                        that.$message({
                            message: "请选择需要上传的文件",
                            type: "warning",
                        });
                        return false;
                    }
                    that.param.append("token", getCookie("token"));
                    that.param.append("acctMonth", that.importForm.year);
                    $budgetAPI.importIctRecordData(
                        that.param,
                        (res) => {
                            if (res.code == 0) {
                                that.$message({
                                    message: "导入成功",
                                    type: "success",
                                });
                                that.getData();
                            } else {
                                that.$message.error(res.message);
                            }
                            that.dialogVisible = false;
                        },
                        (err) => {
                            that.dialogVisible = false;
                        }
                    );
                } else {
                    console.log("error submit!!");
                    return false;
                }
            });
        },
    },
};
</script>

<style lang="scss" scoped>
</style>